package cn.db101.gocentdaily.controller;

import cn.db101.gocentdaily.entity.SysRole;
import cn.db101.gocentdaily.entity.SysUser;
import cn.db101.gocentdaily.model.ResultModel;
import cn.db101.gocentdaily.model.UserModel;
import cn.db101.gocentdaily.service.SysRoleService;
import cn.db101.gocentdaily.service.SysUserService;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zooy
 * @create 2024-06-02 12:34
 * @description
 */
@RestController
@RequestMapping("cn/db101/gocent/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("info")
    public ResultModel info(@RequestParam("token") String token) {

        int userId = NumberUtil.parseInt((String) StpUtil.getLoginIdByToken(token));
        SysUser sysUser = sysUserService.getById(userId);

        // 配置权限
        SysRole sysRole = sysRoleService.getById(sysUser.getRoleId());

        UserModel userModel = new UserModel();
        userModel.setUserId(sysUser.getId());
        userModel.setUsername(sysUser.getUsername());
        userModel.setRoles(ListUtil.of(sysRole.getName()));

        return ResultModel.success(userModel);
    }
}
